package com.czhhx.myuppercase;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private boolean isWan = false;
    private double num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tvText = findViewById(R.id.tvText);
        TextView tvCopy = findViewById(R.id.tvCopy);
        TextView tvWan = findViewById(R.id.tvWan);
        EditText editNum = findViewById(R.id.editNum);
        ImageView tvClan = findViewById(R.id.tvClan);
        EditTextUtils.afterDotTwo(editNum, false);
        tvClan.setOnClickListener(view -> {
            editNum.setText("");
        });
        tvCopy.setOnClickListener(view -> {
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            cm.setText(tvText.getText().toString());
            Toast.makeText(this, "已复制", Toast.LENGTH_SHORT).show();

        });

        tvWan.setOnClickListener(view -> {
            if (isWan) {
                isWan = false;
                EditTextUtils.afterDotTwo(editNum, true);
                tvWan.setBackgroundResource(R.drawable.bg_true);
            } else {
                isWan = true;
                EditTextUtils.afterDotTwo(editNum, false);
                tvWan.setBackgroundResource(R.drawable.bg_false);
            }
        });
        editNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
//                EditTextUtils.afterDotTwo(editNum);
                Log.e("TAG", editable.toString());
                num = Double.parseDouble(editable.toString());
                if (isWan) {
                    tvText.setText(MoneyUtil.toChinese((num * 1000) + ""));
                } else {
                    tvText.setText(MoneyUtil.toChinese(editNum.getText().toString()));
                }
                if (editable.toString().isEmpty()) {
                    tvClan.setVisibility(View.GONE);
                } else {
                    tvClan.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}